from typing import *


class Solution:

    def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
        n = len(graph)
        ans = []

        def f(id, path: list):
            if id == n - 1:
                ans.append(path.copy())
                return
            for next in graph[id]:
                path.append(next)
                f(next, path)
                path.pop()

        f(0, [0])
        return ans